package com.ccf.demomongodb;

import com.ccf.demomongodb.entity.User;
import com.ccf.demomongodb.repository.UserRepository;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import org.joda.time.DateTime;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;

@SpringBootTest
class DemomongoDbApplicationTests1 {

    @Autowired
    private UserRepository userRepository;

    //添加操作
    @Test
    public void add() {
        User user = userRepository
                .save(new User("2", "cjw", 22, "232323@qq.com", new DateTime().toString()));
        System.out.println(user);
    }

    //查询表里所有记录
    @Test
    public void findAll() {
        List<User> all = userRepository.findAll();
        System.out.println(all);
    }

    //id查询表
    @Test
    public void findId() {
        User userbyId = userRepository.findById("2").get();
        System.out.println(userbyId);
    }

    //条件查询表
    @Test
    public void findUserList() {
        User user = new User();
        user.setName("cjw");
        user.setAge(22);
        Example<User> userExample = Example.of(user);

        List<User> all = userRepository.findAll(userExample);
        System.out.println(all);
    }
    //模糊查询表
    @Test
    public void findLikeUserList() {
        //创建匹配器，即如何使用查询条件
        ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //改变默认字符串匹配方式：模糊查询
                .withIgnoreCase(true); //改变默认大小写忽略方式：忽略大小写
        User user = new User();
        user.setName("jw");
        Example<User> userExample = Example.of(user, matcher);
        List<User> userList = userRepository.findAll(userExample);
        System.out.println(userList);
    }

    //分页 查询表
    @Test
    public void findPageUserList() {
        Sort sort = Sort.by(Sort.Direction.DESC, "age");
        //0为第一页
        Pageable pageable = PageRequest.of(0, 3, sort);
        //创建匹配器，即如何使用查询条件
        ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //改变默认字符串匹配方式：模糊查询
                .withIgnoreCase(true); //改变默认大小写忽略方式：忽略大小写
        User user = new User();
        user.setName("jw");
        Example<User> userExample = Example.of(user, matcher);
        //创建实例
        Example<User> example = Example.of(user, matcher);
        Page<User> pages = userRepository.findAll(example, pageable);
        System.out.println(pages);
    }

    //修改
    @Test
    public void updateUser() {
        User user = userRepository.findById("2").get();
        user.setName("cjw2");
        user.setAge(24);
        user.setEmail("883220990@qq.com");
        User save = userRepository.save(user);
        System.out.println(save);
    }

    //删除操作
    @Test
    public void delete() {
        userRepository.deleteById("2");
    }





}
